@page "/Cluster/Inspection"
@using BlazorApp.Pages.Common
@using BlazorApp.Pages.Common.Metadata
@using BlazorApp.Pages.Node
@using Extension
@using k8s.Models
@inherits BlazorApp.Pages.Common.PageBase
<div style="margin: 50px">
    @if (AllResourcesCount is { Count: > 0 })
    {
        <GridRow Class="grid-table">
            <GridCol Span="2">@L["Latest"]:</GridCol>
            <GridCol Span="6">
                @LastInspection.ToLocalTime()
                (
                <AgeView Age="@LastInspection"></AgeView>
                ago)
            </GridCol>
            <GridCol Span="2">@L["Error"]:</GridCol>
            <GridCol Span="6">
                <Badge Count="@AnalyzeResult.Count" ShowZero="true"/>
            </GridCol>
            <GridCol Span="8"></GridCol>
        </GridRow>

        <GridRow Class="grid-table">
            <GridCol Span="8">
                @if (AnalyzeResult is { Count: > 0 })
                {
                    @foreach (var kv in AnalyzeResult.GroupBy(x => x.Kind).Select(g => new { Kind = g.Key, Count = g.Count() }))
                    {
                        <div>
                            @($"{L[kv.Kind]}")
                            <Badge Count="@kv.Count"/>
                        </div>
                    }
                }
                @if (PassResources is { Count: > 0 })
                {
                    @foreach (var name in PassResources)
                    {
                        <div>
                            @($"{L[name]}")
                            <Badge Count="0" ShowZero="true" Style="background-color: #52c41a; "/>
                        </div>
                    }
                }
            </GridCol>
            <GridCol Span="16">
                @if (Ai.Enabled())
                {
                    if (AnalyzeResult is { Count: > 0 })
                    {
                        <Button Type="@ButtonType.Default" OnClick="@OnSummaryClick">@L["Diagnosis"]</Button>
                        @if (!_aiSummary.IsNullOrWhiteSpace())
                        {
                            <Alert Type="@AlertType.Warning">
                                @((MarkupString)_aiSummary.ToHtmlDisplay())
                            </Alert>
                        }
                    }
                }
            </GridCol>
        </GridRow>
    }
    else
    {
        <Spin/>
    }
</div>
<Tabs Style="margin: 50px" DefaultActiveKey="Overview" Type="@TabType.Card" Size="@TabSize.Large">

    <TabPane Key="Overview" Tab=@L["Overview"]>

        @if (AllResourcesCount is { Count: > 0 })
        {
            <Space Wrap>
                @foreach (var item in AllResourcesCount)
                {
                    var errors = AnalyzeResult.Where(x => x.Kind == item.Key).ToList().Count;
                    <SpaceItem>
                        <Card Style="width: 300px;margin: 5px">
                            <GridRow Class="grid-table">
                                <GridCol Span="16">
                                    <div >
                                        @item.Key
                                    </div >
                                    <div >
                                        @($"{errors}  / {item.Value}  ")
                                        <span style="color: gray">
                                            @($"[err/all]")
                                        </span >
                                    </div >
                                </GridCol >
                                <GridCol Span="8">
                                    <InspectionResourceRingChart Denominator="@item.Value" Numerator="@errors"></InspectionResourceRingChart >
                                </GridCol >
                            </GridRow>
                        </Card>
                    </SpaceItem>
                }
            </Space>
        }
        else
        {
            <Empty Simple/>
        }
    </TabPane>

    <TabPane Key="Livez" Tab=@L["LivenessProbe"]>
        @((MarkupString)LivezResult.ToHtmlDisplay())
    </TabPane>
    <TabPane Key="Readyz" Tab=@L["ReadinessProbe"]>
        @((MarkupString)ReadyzResult.ToHtmlDisplay())
    </TabPane>
    <TabPane Key="Node" Tab=@L["Node"]>
        @if (NodeList is { Count: > 0 })
        {
            <GridRow Class="grid-table">
                <GridCol Span="24">
                    <Space Size="@("4")" Wrap>
                        @foreach (var item in NodeList)
                        {
                            <SpaceItem>
                                <Card Title="@item.Name()" Style="margin: 10px;width: 380px">
                                    <Extra>
                                        <Button OnClick="@(() => OnItemNameClick(item))"
                                                Type="@ButtonType.Link">
                                            @L["Detail"]
                                        </Button>
                                    </Extra>
                                    <Body>
                                    <NodeStatusChart Node="@item"></NodeStatusChart>
                                    </Body>
                                </Card>
                            </SpaceItem>
                        }
                    </Space>
                </GridCol>
            </GridRow>
        }
        else
        {
            <Empty Simple/>
        }
    </TabPane>
    <TabPane Key="Pod" Tab=@L["Pod"]>
        @if (PodList is { Count: > 0 })
        {
            <GridRow Class="grid-table">
                <GridCol Span="8">Name</GridCol>
                <GridCol Span="8">CPU</GridCol>
                <GridCol Span="8">Memory</GridCol>
            </GridRow>
            @foreach (var item in PodList)
            {
                <GridRow Class="grid-table grid-table-first-row">
                    <GridCol Span="8">@item.Name()</GridCol>
                    <GridCol Span="8">
                        <MetricView ResourceType="Pod" ItemName="@item.Name()" MetricType="cpu"></MetricView>
                    </GridCol>
                    <GridCol Span="8">
                        <MetricView ResourceType="Pod" ItemName="@item.Name()" MetricType="memory"></MetricView>
                    </GridCol>
                </GridRow>
            }
        }
        else
        {
            <Empty Simple/>
        }
    </TabPane>

    <TabPane Key="Errors" Tab=@L["Error"]>

        @if (AnalyzeResult is { Count: > 0 })
        {
            <GridRow Class="grid-table">
                <GridCol Span="8">@L["Name"]</GridCol>
                <GridCol Span="12">@L["Error"]</GridCol>
                <GridCol Span="4">@L["Action"]</GridCol>

            </GridRow>
            @foreach (var item in AnalyzeResult)
            {
                <GridRow Class="grid-table grid-table-first-row">

                    <GridCol Span="8">
                        <RefView Ref="@GetRef(item)" FullView="true"></RefView>
                    </GridCol>
                    <GridCol Span="12">
                        <div>
                            @foreach (var failure in item.Error)
                            {
                                <li style="list-style: decimal">@failure.Text</li>
                            }
                        </div>
                    </GridCol>
                    <GridCol Span="4">
                        @if (Ai.Enabled())
                        {
                            <Button OnClick="@(() => OnAnalyzeClick(item))">@L["AI Analysis"]</Button>
                            <a class="ant-btn ant-btn-default"
                               href="/Ai/Workflow/@($"请尝试修复{item.Namespace()}命名空间里名为{item.Name()}的{item.Kind}")"
                               target="_blank">
                                @L["Try Fix"]
                            </a>
                        }
                    </GridCol>
                </GridRow>
            }
        }
        else
        {
            <Empty Simple/>
        }
    </TabPane>


</Tabs >
